/**
 * Created by yangxianming on 2020/12/30.
 */
var actions = [""];
//
//接口定义 都是以api开头 所需参数都通过方法传入
//function apiGetCorpList(dofun, envText){}
//

//获取已归档项目列表数据
function apiShowFileList(dofun){
    var xmlHttp = new XMLHttpRequest();
    //year  progressName
    var year = document.getElementById("year").value;
    var progress = document.getElementById("progress");
    var progressValue = selectedText(progress);
    var value = "action=forFile&year="+year+"&progressName="+progressValue;
    xmlHttp.open("get", "/progress/showProgress?" + value, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}


//钉钉推送
function apiDDPush(id){
    if(!confirm("报告将推送到领导群，是否要继续？")){
        return;
    }
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/progress/showProgress?action=forMobile&dd=1&id="+id, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    alert("发送成功")
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}

//获取项目列表数据
function apiShowList(dofun){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/progress/showProgress?action=forWeb", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    for(var i=0; i<r["result"].length; i++){
                        dofun(r["result"][i]);
                    }
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}

//新建项目
function apiNewProgress(){
    var saveData = {"action": "newProgress"};
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/progress/saveProgress", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    //创建成功后刷新页面
                    initializeData();
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(saveData));
}

//保存进度表, 归档进度表
function apiSaveProgress(state, divTop){
    var saveData = {};
    saveData["id"] = divTop.getAttribute("id");
    var data = [];

    // state=1 保存； state=2 归档
    if(state == 1){
        var progressName = divTop.getElementsByTagName("div")[0].getElementsByTagName("input")[0].value;
        var divFirst =  divTop.getElementsByTagName("div")[2];
        var table = divFirst.getElementsByTagName("table")[0];
        var tbody = table.getElementsByTagName("tbody")[0];
        var trArr = tbody.getElementsByTagName("tr");
        saveData["progressName"] = progressName;
        saveData["action"] = "update";
        for(var i=1; i<trArr.length-1; i++){
            var tdArr = trArr[i].getElementsByTagName("td");
            var oneData = [];
            //1
            //模块为空，将不会保存
            var module = tdArr[0].getElementsByTagName("input")[0].value.trim();
            if(module == ""){
                continue;
            }
            oneData.push(module);
            //2
            oneData.push(tdArr[1].getElementsByTagName("textarea")[0].value.trim());
            //3
            oneData.push(tdArr[2].getElementsByTagName("input")[0].value.trim());
            //4
            if(tdArr[3].getElementsByTagName("input")[0].checked){
                oneData.push("y");
            }else{
                oneData.push("n");
            }
            //5
            oneData.push(tdArr[4].getElementsByTagName("input")[0].value.trim());
            //6
            oneData.push(selectedText(tdArr[5].getElementsByTagName("select")[0]));
            //7
            oneData.push(selectedText(tdArr[6].getElementsByTagName("select")[0]));
            //8
            oneData.push(selectedText(tdArr[7].getElementsByTagName("select")[0]));
            //9
            oneData.push(selectedText(tdArr[8].getElementsByTagName("select")[0]));
            //10
            oneData.push(tdArr[9].getElementsByTagName("input")[0].value.trim());
            //11
            oneData.push(tdArr[10].getElementsByTagName("input")[0].value.trim());

            data.push(oneData);
        }

        saveData["detail"] = data;

        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("post", "/progress/saveProgress", true);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState==4){
                if (xmlHttp.status==200){
                    var r = JSON.parse(xmlHttp.response);
                    if (r["apiStatus"] == "1"){
                        initializeData();
                        alert("数据已保存");
                    }
                    if (r["apiStatus"] == "0"){
                        alert(r["errorMsg"]);
                    }
                }
            }
        };
        xmlHttp.send(JSON.stringify(saveData));
    }

    //state=2 归档
    if(state == 2){
        if(!confirm("归档后页面将不再显示")){
            return;
        }
        saveData["action"] = "file";
        var xmlHttp1 = new XMLHttpRequest();
        xmlHttp1.open("post", "/progress/saveProgress", true);
        xmlHttp1.onreadystatechange = function(){
            if (xmlHttp1.readyState==4){
                if (xmlHttp1.status==200){
                    var r = JSON.parse(xmlHttp1.response);
                    if (r["apiStatus"] == "1"){
                        initializeData();
                    }
                    if (r["apiStatus"] == "0"){
                        alert(r["errorMsg"]);
                    }
                }
            }
        };
        xmlHttp1.send(JSON.stringify(saveData));
    }

}

//

//
//页面渲染 都是以draw开头
//function drawGetCorpList(data){}
//
function drawFileList(data){
    var th = document.getElementById("th");
    var progressList = document.getElementById("progressList");
    progressList.innerText = "";
    progressList.appendChild(th);

    for (var i=0; i<data.length; i++){
        var getDate = data[i][2];
        var yearFull = getDate.split(" ");
        var year = yearFull[0];
        var progressname = data[i][0];
        var isfiledetail = JSON.parse(data[i][1].replace(/[\r\n\s+]/g, '|'));
        var replay = data[i][3];

        var tr = document.createElement("tr");
        var td1 = document.createElement("td");
        td1.innerText = year;
        tr.appendChild(td1);

        var td2 = document.createElement("td");
        td2.innerText = progressname;
        tr.appendChild(td2);

        var td3 = document.createElement("td");
        var isfiledetailStr = "";
        for (var ii=0; ii<isfiledetail["content"].length; ii++){
            var ind = ii + 1;
            isfiledetailStr = isfiledetailStr + ind + "、" + isfiledetail["content"][ii] + "。";
        }
        td3.innerText = "【模块】" + isfiledetail["modules"] + " \n 【具体内容】"  + isfiledetailStr;
        tr.appendChild(td3);

        var td4 = document.createElement("td");
        td4.innerText = isfiledetail["case"];
        tr.appendChild(td4);

        var td5 = document.createElement("td");
        td5.innerText = isfiledetail["bug"];
        tr.appendChild(td5);

        var td6 = document.createElement("td");
        if(replay != ""){
            var a6 = document.createElement("a");
            a6.setAttribute("href", replay);
            a6.setAttribute("target", "_blank");
            a6.innerText = "查看";
            td6.appendChild(a6);
        }
        tr.appendChild(td6);



        progressList.appendChild(tr);
    }

}

//预览进度表
function drawView(id){
    window.open("/html/progressmobile.html?id="+id);
}

//绘制单个项目列表
function drawProgress(data){
    var getProgressName = "";
    var getId = "";
    var getDetail = ["","","","","","","","","","",""];
    if(data == undefined){
        //新建一个项目并获得id
        apiNewProgress();
        return;
    }else{
        getId = data["id"];
        getProgressName = data["progressname"];
        getDetail = JSON.parse(data["detail"]);
    }


    var tableHeader = ["主要模块","迭代内容","前后端开发","是否提测","测试负责","设计用例","用例执行","提交问题","修复问题","测试进度%","备注"];
    var content = document.getElementsByClassName("content")[0];
    var divTop = document.createElement("div");
    divTop.style.width = "100%";
    divTop.setAttribute("id", getId);

    //----------
    var div1 = document.createElement("div");
    var input = document.createElement("input");
    div1.style.fontSize = "large";
    div1.style.width = "50%";
    div1.style.marginTop = "10px";
    input.className = "progress";
    input.type = "text";
    input.value = getProgressName;
    div1.appendChild(input);
    var div1_1 = document.createElement("div");
    div1_1.innerText = " -- 总体测试进度表";
    div1.appendChild(div1_1);
    divTop.appendChild(div1);

    //----------
    var div2 = document.createElement("div");
    div2.style.width = "100%";
    var table = document.createElement("table");
    table.border = 1;
    table.style.textAlign = "center";
    table.style.width = "100%";
    div2.appendChild(table);
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    var tr = document.createElement("tr");
    tbody.appendChild(tr);
    for(var i=0; i<tableHeader.length; i++){
        var th = document.createElement("th");
        th.innerText = tableHeader[i];
        if(i == 0){
            //th.style.width = "100px";
        }
        if(i == 1){
            //th.style.width = "500px";
        }
        if(i == 2){
            th.style.width = "100px";
        }
        if(i == 3){
            th.style.width = "40px";
        }
        if(i == 4){
            th.style.width = "100px";
        }
        if(i == 5){
            th.style.width = "60px";
        }
        if(i == 6){
            th.style.width = "60px";
        }
        if(i == 7){
            th.style.width = "60px";
        }
        if(i == 8){
            th.style.width = "60px";
        }
        if(i == 9){
            th.style.width = "70px";
        }
        tr.appendChild(th);
    }

    //加载内部数据
    for(var x=0; x<getDetail.length; x++){
        drawInnerData(tbody, getDetail[x])
    }

    //尾部+按钮
    var tdAdd = document.createElement("td");
    var trAdd = document.createElement("tr");
    trAdd.setAttribute("t", "tail");
    tdAdd.setAttribute("style", "text-align:center;cursor:pointer;");
    tdAdd.setAttribute("colspan", "11");
    tdAdd.onclick = function(){drawAddAction(tbody)};
    tdAdd.innerText = "+";
    trAdd.appendChild(tdAdd);
    tbody.appendChild(trAdd);
    tbody.appendChild(trAdd);
    divTop.appendChild(div2);

    //----------
    var div3 = document.createElement("div");

    var b1 = document.createElement("button");
    b1.title = "查看发送钉钉推送后的页面样式";
    b1.innerText = "预览";
    b1.onclick = function(){drawView(getId)};
    div3.appendChild(b1);

    var b2 = document.createElement("button");
    b2.title = "归档的项目将用永久保存数据库并从前端删除";
    b2.innerText = "归档";
    b2.onclick = function(){apiSaveProgress("2", divTop)};
    div3.appendChild(b2);

    var b3 = document.createElement("button");
    b3.title = "保存数据到服务器，依然会在前端显示";
    b3.innerText = "保存";
    b3.onclick = function(){apiSaveProgress("1", divTop)};
    div3.appendChild(b3);

    var b4 = document.createElement("button");
    b4.title = "将概要数据发送到钉钉群";
    b4.innerText = "推送";
    b4.onclick = function(){apiDDPush(getId)};
    div3.appendChild(b4);

    var b5 = document.createElement("button");
    b5.title = "跳转相关测试用例";
    b5.innerText = "用例";
    b5.onclick = function(){
        var p = this.parentNode.parentNode.getElementsByTagName("div")[0].getElementsByTagName("input")[0].value;
        createPage("progressCase", p);
    };
    div3.appendChild(b5);

    var b6 = document.createElement("button");
    b6.title = "跳转相关问题页面";
    b6.innerText = "问题";
    b6.onclick = function(){
        var p = this.parentNode.parentNode.getElementsByTagName("div")[0].getElementsByTagName("input")[0].value;
        createPage("progressBug", p);
    };
    div3.appendChild(b6);

    divTop.appendChild(div3);
    content.appendChild(divTop);
}

//绘制内部数据
function drawInnerData(tbody, data){
    var trNew = document.createElement("tr");
    tbody.appendChild(trNew);
    //第1列
    var tdNew1 = document.createElement("td");
    trNew.appendChild(tdNew1);
    var inputNew1 = document.createElement("input");
    inputNew1.className = "forTD";
    inputNew1.value = data[0];
    inputNew1.title = data[0];
    tdNew1.appendChild(inputNew1);

    //第2列
    var tdNew2 = document.createElement("td");
    trNew.appendChild(tdNew2);
    var inputNew2 = document.createElement("textarea");
    inputNew2.className = "forTD";
    inputNew2.value = data[1];
    var heightC = parseInt(data[1].length/35) * 22 + 22;
    inputNew2.style.height = heightC + "px";
    tdNew2.appendChild(inputNew2);

    //第3列
    var tdNew3 = document.createElement("td");
    trNew.appendChild(tdNew3);
    var inputNew3 = document.createElement("input");
    inputNew3.className = "forTD";
    inputNew3.value = data[2];
    tdNew3.appendChild(inputNew3);

    //第4列
    var tdNew4 = document.createElement("td");
    trNew.appendChild(tdNew4);
    var inputBox = document.createElement("input");
    inputBox.setAttribute("class", "checkboxForTD");
    inputBox.type = "checkbox";
    inputBox.title = "选中就是已经提测";
    if(data[3] == "y"){
        inputBox.checked = "checked";
    }

    tdNew4.appendChild(inputBox);
    //第5列
    var tdNew5 = document.createElement("td");
    trNew.appendChild(tdNew5);
    var inputNew4 = document.createElement("input");
    inputNew4.className = "forTD";
    inputNew4.value = data[4];
    tdNew5.appendChild(inputNew4);

    //第6列
    var tdNew6 = document.createElement("td");
    trNew.appendChild(tdNew6);
    var select1 = document.createElement("select");
    select1.setAttribute("class", "forTD");
    select1.onchange = function(){};
    loadSelectList(select1, actions);
    setSelectChecked(select1, data[5]);
    tdNew6.appendChild(select1);

    //第7列
    var tdNew7 = document.createElement("td");
    trNew.appendChild(tdNew7);
    var select2 = document.createElement("select");
    select2.setAttribute("class", "forTD");
    select2.onchange = function(){};
    loadSelectList(select2, actions);
    setSelectChecked(select2, data[6]);
    tdNew7.appendChild(select2);

    //第8列
    var tdNew8 = document.createElement("td");
    trNew.appendChild(tdNew8);
    var select3 = document.createElement("select");
    select3.setAttribute("class", "forTD");
    select3.onchange = function(){};
    loadSelectList(select3, actions);
    setSelectChecked(select3, data[7]);
    tdNew8.appendChild(select3);

    //第9列
    var tdNew9 = document.createElement("td");
    trNew.appendChild(tdNew9);
    var select4 = document.createElement("select");
    select4.setAttribute("class", "forTD");
    select4.onchange = function(){};
    loadSelectList(select4, actions);
    setSelectChecked(select4, data[8]);
    tdNew9.appendChild(select4);

    //第10列
    var tdNew10 = document.createElement("td");
    trNew.appendChild(tdNew10);
    var inputNew5 = document.createElement("input");
    inputNew5.className = "forTD";
    inputNew5.value = data[9];
    tdNew10.appendChild(inputNew5);

    //第11列
    var tdNew11 = document.createElement("td");
    trNew.appendChild(tdNew11);
    var inputNew6 = document.createElement("input");
    inputNew6.className = "forTD";
    inputNew6.value = data[10];
    tdNew11.appendChild(inputNew6);

}

//绘制+按钮
function drawAddAction(tbody){
    var tr = tbody.getElementsByTagName("tr");
    tbody.removeChild(tbody.getElementsByTagName("tr")[tr.length-1]);
    var th = tr[0].getElementsByTagName("th");

    var trNew = document.createElement("tr");
    tbody.appendChild(trNew);
    //第1列
    var tdNew1 = document.createElement("td");
    trNew.appendChild(tdNew1);
    var inputNew1 = document.createElement("input");
    inputNew1.className = "forTD";
    tdNew1.appendChild(inputNew1);
    //第2列
    var tdNew2 = document.createElement("td");
    trNew.appendChild(tdNew2);
    var inputNew2 = document.createElement("textarea");
    inputNew2.className = "forTD";
    tdNew2.appendChild(inputNew2);
    //第3列
    var tdNew3 = document.createElement("td");
    trNew.appendChild(tdNew3);
    var inputNew3 = document.createElement("input");
    inputNew3.className = "forTD";
    tdNew3.appendChild(inputNew3);
    //第4列
    var tdNew4 = document.createElement("td");
    trNew.appendChild(tdNew4);
    var inputBox = document.createElement("input");
    inputBox.setAttribute("class", "checkboxForTD");
    inputBox.type = "checkbox";
    inputBox.title = "选中就是已经提测";
    tdNew4.appendChild(inputBox);
    //第5列
    var tdNew5 = document.createElement("td");
    trNew.appendChild(tdNew5);
    var inputNew4 = document.createElement("input");
    inputNew4.className = "forTD";
    tdNew5.appendChild(inputNew4);
    //第6列
    var tdNew6 = document.createElement("td");
    trNew.appendChild(tdNew6);
    var select1 = document.createElement("select");
    select1.setAttribute("class", "forTD");
    select1.onchange = function(){};
    loadSelectList(select1, actions);
    tdNew6.appendChild(select1);
    //第7列
    var tdNew7 = document.createElement("td");
    trNew.appendChild(tdNew7);
    var select2 = document.createElement("select");
    select2.setAttribute("class", "forTD");
    select2.onchange = function(){};
    loadSelectList(select2, actions);
    tdNew7.appendChild(select2);
    //第8列
    var tdNew8 = document.createElement("td");
    trNew.appendChild(tdNew8);
    var select3 = document.createElement("select");
    select3.setAttribute("class", "forTD");
    select3.onchange = function(){};
    loadSelectList(select3, actions);
    tdNew8.appendChild(select3);
    //第9列
    var tdNew9 = document.createElement("td");
    trNew.appendChild(tdNew9);
    var select4 = document.createElement("select");
    select4.setAttribute("class", "forTD");
    select4.onchange = function(){};
    loadSelectList(select4, actions);
    tdNew9.appendChild(select4);
    //第10列
    var tdNew10 = document.createElement("td");
    trNew.appendChild(tdNew10);
    var inputNew5 = document.createElement("input");
    inputNew5.className = "forTD";
    tdNew10.appendChild(inputNew5);
    //第11列
    var tdNew11 = document.createElement("td");
    trNew.appendChild(tdNew11);
    var inputNew6 = document.createElement("input");
    inputNew6.className = "forTD";
    tdNew11.appendChild(inputNew6);


    var tdAddNew = document.createElement("td");
    var trAddNew = document.createElement("tr");
    trAddNew.setAttribute("t", "tail");
    tdAddNew.setAttribute("style", "text-align:center;cursor:pointer;");
    tdAddNew.setAttribute("colspan", th.length.toString());
    tdAddNew.onclick = function(){drawAddAction(tbody)};
    tdAddNew.innerText = "+";
    trAddNew.appendChild(tdAddNew);
    tbody.appendChild(trAddNew);
}

//
//页面调用 数据获取，接口层与渲染层的连接
//
function controlTopDivProgress(whichWindow, mySwitch, text){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        //设置页面抬头
        topDiv.getElementsByTagName("label")[0].innerText = text;
        //加载数据
        apiShowFileList(drawFileList);
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
    }

    //创建和编辑功能判断
    if (text == "添加用例"){}
    if (text == "编辑用例"){}
    if (mySwitch == "close") topDiv.style.display = "none";
}

function initializeData(){
    for(var i=1; i<101; i++){
        actions.push(i);
    }
    var content = document.getElementsByClassName("content")[0];
    content.innerText = "";

    apiShowList(drawProgress);

    //查询历史档案页面 填充当前年份
    var da = new Date();
    document.getElementById("year").value = da.getFullYear();

    //查询历史档案页面 填充项目下拉
    var progressList = ["", "福员外", "口福"];
    loadSelectList(document.getElementById("progress"), progressList)
}